package mapControl;

import java.util.Queue;
import java.util.Random;

import net.datastructures.Position;
import net.datastructures.PositionList;
import powerUp.BombPowerUp;
import powerUp.BombUp;
import powerUp.PowerUp;
import powerUp.SpeedPowerUp;
import wallFactory.Factory;
import wallFactory.builders.DestWallBulider;
import wallFactory.builders.NonDestWallBulider;
import wallFactory.builders.WallBox;

import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jme.system.DisplaySystem;
import com.jmex.effects.particles.ParticleMesh;
import com.jmex.physics.PhysicsNode;
import com.jmex.physics.PhysicsSpace;

import elementos.Bomba;
import elementos.Player;
import enemies.Enemy;
import explosionFactory.ExplosionFactory;

public class GraphicsMapControl extends MapControl {

	Node rootNode;
	PhysicsSpace physicsSpace;
	DisplaySystem display;
	Queue<PhysicsNode> trashColector;

	public GraphicsMapControl(int wide, int high, int tilesize, Node rootNode,
			PhysicsSpace physicsSpace, DisplaySystem display,
			Queue<PhysicsNode> trashColector) {
		super(wide, high, tilesize);
		this.rootNode = rootNode;
		this.physicsSpace = physicsSpace;
		this.display = display;
		this.trashColector = trashColector;
		rnd = new Random();
	}

	public void buildMap() {
		Factory fabricaElementos = new Factory();
		DestWallBulider dwb = new DestWallBulider(this);
		NonDestWallBulider ndwb = new NonDestWallBulider(this);

		Random rnd = new Random();
		for (int row = 0; row < wide; row++) {

			for (int col = 0; col < hight; col++) {

				crearCelda(row, col);

				if ((row % 2 == 0 && col % 2 == 0) || row == 0 || col == 0
						|| row == wide - 1 || col == hight - 1) {
					fabricaElementos.setWallBuilder(ndwb);
					fabricaElementos.construct(row, col);

				} else if (row == 1 && col == 1) { // player
				} else if (row == wide - 2 && col == hight - 2) { // sirius
				} else if (row == wide - 2 && col == hight - 3) { // empty
				} else if (row == wide - 3 && col == hight - 2) { // empty
				} else if (row == 1 && col == 2) { // empty
				} else if (row == 2 && col == 1) { // empty
				} else if (rnd.nextInt(100) < 20) {
					fabricaElementos.setWallBuilder(dwb);
					fabricaElementos.construct(row, col);

				}
			}
		}
		super.buildGraph();
	}

	@Override
	public void crearCelda(int row, int col) {
		super.crearCelda(row, col);

	}

	public PhysicsSpace getPhysicsSpace() {
		return physicsSpace;
	}

	public Node getRootNode() {
		return rootNode;
	}

	public DisplaySystem getDisplay() {
		return display;
	}

	Random rnd;

	public boolean detonate(int row, int col) {

		if ((row > 0 && col > 0) && (row < wide - 1 && col < hight - 1)) {

			if (map[row][col].getDesWallBox() != null) {
				removeWall(map[row][col].getDesWallBox());

				int rn = rnd.nextInt(200);
				if (rn < 80 && rn > 40)
					dropear(row, col);

			} else if (map[row][col].getNonDesWallBox() != null) {
				return false;

			} else if (map[row][col].getBomba() != null) {
				map[row][col].getBomba().detonar();
			} else if (!map[row][col].getEnemies().isEmpty()) {
				PositionList<Enemy> enemies = map[row][col].getEnemies();
				Position<Enemy> pos = enemies.first();
				while (pos  != enemies.last()){
					pos.element().kill();
					pos = enemies.next(pos);
				}
				pos.element().kill();

			}
			explosion(getTraslationCentrada(row, col));
		}
		return true;
	}

	private void dropear(int row, int col) {
		System.out.println("salio pota");

		int rn = rnd.nextInt(600);
		PowerUp powerUp = null;

		if (rn < 200)
			powerUp = new SpeedPowerUp(row, col, this);
		else if (rn < 400)
			powerUp = new BombPowerUp(row, col, this);
		else if (rn < 600)
			powerUp = new BombUp(row, col, this);

		addPowerUp(powerUp);

	}

	@Override
	public void addPowerUp(PowerUp powerUp) {
		super.addPowerUp(powerUp);
	}

	public void explosion(Vector3f pos) {
		ParticleMesh explosion = ExplosionFactory.getSmallExplosion();
		explosion.setOriginOffset(pos.clone());
		explosion.forceRespawn();
		rootNode.attachChild(explosion);
	}

	@Override
	public void removeWall(WallBox wall) {
		super.removeWall(wall);
		trashColector.add(wall.getNode());

	}

	@Override
	public void removeBomb(Bomba elem) {
		super.removeBomb(elem);
		trashColector.add(elem.getNode());

	}

	@Override
	public void addPlayer(Player player) {
		super.addPlayer(player);

	}

	public boolean HayPowerUp(int row, int col) {

		return map[row][col].getPowerUp() != null;
	}

	public PowerUp getPowerUp(int row, int col) {
		return map[row][col].getPowerUp();

	}

	@Override
	public void removePowerUp(PowerUp powerUp) {
		super.removePowerUp(powerUp);
		trashColector.add(powerUp.getNode());

	}

	public void incSpeed() {
		speed += 10;

	}

	public float getSpeed() {
		return speed;
	}

	public void incBombPower() {
		bombPower++;
	}

	public int getBombPower() {
		return bombPower;
	}

	@Override
	public void removeEnemy(Enemy enemy) {
		super.removeEnemy(enemy);
		trashColector.add(enemy.getNode());

	}



}
